package org.xshop.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.transaction.annotation.Transactional;
import org.xshop.mapper.TMemberCollectionMapper;
import org.xshop.model.TGoods;
import org.xshop.model.TGoodsPromotion;
import org.xshop.model.TMemberCollection;
import org.xshop.service.ITGoodsService;
import org.xshop.service.ITMemberCollectionService;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weibo.api.motan.config.springsupport.annotation.MotanService;

import top.ibase4j.core.base.BaseServiceImpl;
import top.ibase4j.core.support.Pagination;
import top.ibase4j.core.util.DataUtil;
import top.ibase4j.core.util.DateUtil;
import top.ibase4j.core.util.InstanceUtil;

/**
 * <p>
 * 我的收藏 服务实现类
 * </p>
 *
 * @author ShenHuaJie
 * @since 2018-03-01
 */
@MotanService(interfaceClass = ITMemberCollectionService.class)
@CacheConfig(cacheNames = "TMemberCollection")
public class MemberCollectionService extends BaseServiceImpl<TMemberCollection, TMemberCollectionMapper>
implements ITMemberCollectionService {

    @Autowired
    private ITGoodsService goodsService;

    @Override
    public Object queryAll(Map<String, Object> param) {
        Object pageNum = param.get("pageNum");
        if (pageNum != null) {
            param.put("pageIndex", pageNum);
        }
        Pagination<TMemberCollection> page = super.query(param);
        List<TMemberCollection> collections = page.getRecords();
        // 商店归属
        Map<Long, List<TGoods>> map = InstanceUtil.newLinkedHashMap();
        for (int i = 0; i < collections.size(); i++) {
            TMemberCollection collection = collections.get(i);
            TGoods goods = goodsService.queryById(collection.getCollectionId());
            if (goods != null) {
                if (goods.getIsPromotion() == 1) {
                    TGoodsPromotion promotion = goods.getPromotion();
                    if (promotion != null) {
                        Date now = DateUtil.stringToDate(DateUtil.getDate());
                        if (!promotion.getStartTime().after(now) && !promotion.getEndTime().before(now)) {
                            goods.setSalePrice(promotion.getPrice());
                        }
                    }
                }
                Long id = goods.getStore().getId();
                if (map.containsKey(id)) {
                    map.get(id).add(goods);
                } else {
                    List<TGoods> list = InstanceUtil.newArrayList();
                    list.add(goods);
                    map.put(id, list);
                }
            }
        }
        JSONArray shopArr = new JSONArray();
        Set<Long> set = map.keySet();
        Iterator<Long> iterator = set.iterator();
        while (iterator.hasNext()) {
            Long id = iterator.next();
            List<TGoods> goods = map.get(id);
            JSONObject goodsObj = (JSONObject) JSON.toJSON(goods.get(0).getStore());
            for (TGoods tGoods : goods) {
                tGoods.setStore(null);
            }
            goodsObj.put("goods", goods);

            shopArr.add(goodsObj);
        }
        return shopArr;
    }

    @Override
    @Transactional
    public Object delAll(TMemberCollection param) {
        if (DataUtil.isNotEmpty(param.getGoodsIds())) {
            String[] ids = param.getGoodsIds().split(",");
            TMemberCollection collection = new TMemberCollection();
            collection.setMemberId(param.getMemberId());
            for (String string : ids) {
                collection.setCollectionId(Long.parseLong(string));
                deleteByEntity(collection);
            }
        }
        return null;
    }

}